﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Hinacom.Scheduler.Configuration;
using System.Collections;
using System.Reflection;
using Hinacom.Scheduler;
using Hinacom.Scheduler.Manager.Views;
using Hinacom.Scheduler.Service.Proxy;

namespace Hinacom.Scheduler.Manager
{
    public partial class TaskInfoForm : Form
    {
        public TaskInfoForm()
        {
            InitializeComponent();

            drpJobType.DataSource = new ArrayList(SchedulerConfiguration.Current.JobTypes);
        }
        public TaskInfoForm(JobInfo job) : this()
        {
        }

        private Control currentScheduleView = null;

        private void btnSave_Click(object sender, EventArgs e)
        {
            JobInfo job = null;
            switch (drpScheduleType.SelectedIndex)
            {
                case 0:
                    job = new SimpleJobInfo();
                    break;
            }

            job.Name = txtName.Text;
            job.Group = txtGroup.Text;
            job.Description = txtDescription.Text;
            if (checkBox1.Checked)
                job.StartTimeUtc = dateTimePicker1.Value.ToUniversalTime();
            if (checkBox2.Checked)
                job.EndTimeUtc = dateTimePicker2.Value.ToUniversalTime();
            job.JobTypeName = ((JobTypeConfiguration)drpJobType.SelectedItem).Name;

            ((IScheduleView)currentScheduleView).FillJobInfo(job);

            new SchedulerProxy().CreateJob(job);

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void drpJobType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var jobConfig = (JobTypeConfiguration)drpJobType.SelectedItem;
            lblJobTypeDescription.Text = jobConfig.Type;
            string[] typeParts = jobConfig.Type.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Where(s => !String.IsNullOrWhiteSpace(s)).Select(s => s.Trim()).ToArray();
            Assembly ass = Assembly.Load(typeParts[1]);
            Type jobType = ass.GetType(typeParts[0]);
            Type parameterType = null;
            JobParameterAttribute attr = (JobParameterAttribute)Attribute.GetCustomAttribute(jobType, typeof(JobParameterAttribute));
            if (attr != null)
            {
                parameterType = attr.Type;
            }

            if (parameterType != null)
            {
                pnlSpecifiedParameter.Visible = true;
                var parameter = parameterType.GetConstructor(new Type[0]).Invoke(new object[0]);
                propertyGrid1.SelectedObject = parameter;
            }
            else
            {
                pnlSpecifiedParameter.Visible = false;
                propertyGrid1.SelectedObject = null;
            }
        }

        private void drpScheduleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (currentScheduleView != null)
            {
                pnlSchedule.Controls.Remove(currentScheduleView);
                currentScheduleView = null;
            }

            switch (drpScheduleType.SelectedIndex)
            {
                case 0:
                    currentScheduleView = new SimpleScheduleView();
                    break;
            }
            if (currentScheduleView != null)
            {
                currentScheduleView.Dock = DockStyle.Fill;
                pnlSchedule.Controls.Add(currentScheduleView);
            }
        }
    }
}
